package pong;
// Fig. 27.7: Client.java
// Client portion of a stream-socket connection between client and server.
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import pong.Communication;

public class Client //extends JFrame 
{
    //private JTextField enterField; // enters information from user
    //private JTextArea displayArea; // display information to user

    private ObjectOutputStream output; // output stream to server
    private ObjectInputStream input; // input stream from server
    //private String message = ""; // message from server
    private String chatServer; // host server for this application
    private Socket client; // socket to communicate with server

    Timer t;
    // initialize chatServer and set up GUI
    public Client(String host) {
        chatServer = host; // set server to which this client connects

        ActionListener al = new ActionListener() {
            // send message to server
            public void actionPerformed(ActionEvent event)//tells us if ball bounce or panel moved
            {
                Communication comm = new Communication(new Point(4, 7), Sender.Ball);
                sendData(comm);
            } // end method actionPerformed
        }; // end anonymous inner class
  
         t = new Timer(100, al);
        
    } // end Client constructor

    // connect to server and process messages from server
    public void runClient() {
        try // connect to server, get streams, process connection
        {
            connectToServer(); // create a Socket to make connection
            getStreams(); // get the input and output streams
            t.start();
            processConnection(); // process connection
        } // end try
        catch (EOFException eofException) {
            eofException.printStackTrace();
        } // end catch
        catch (IOException ioException) {
            ioException.printStackTrace();
        } // end catch
        finally {
            closeConnection(); // close connection
        } // end finally
    } // end method runClient

    // connect to server
    private void connectToServer() throws IOException {
        // create Socket to make connection to server
        client = new Socket(InetAddress.getByName(chatServer), Consts.port);
    } // end method connectToServer

    // get streams to send and receive data
    private void getStreams() throws IOException {
        // set up output stream for objects
        output = new ObjectOutputStream(client.getOutputStream());
        output.flush(); // flush output buffer to send header information

        // set up input stream for objects
        input = new ObjectInputStream(client.getInputStream());

//      displayMessage( "\nGot I/O streams\n" );
    } // end method getStreams

    // process connection with server
    private void processConnection() throws IOException {
        Communication message = null;
        do // process messages sent from server
        {
            try // read message and display it
            {
                message = (Communication) input.readObject();
                System.out.println("RECEIVED IN CLIENT" + message);// read new message
            } // end try
            catch (ClassNotFoundException classNotFoundException) {
                classNotFoundException.printStackTrace();
            } // end catch

        } while (!message.equals(new Communication(new Point(0, 0), null)));
    } // end method processConnection

    // close streams and socket
    private void closeConnection() {
        try {
            output.close(); // close output stream
            input.close(); // close input stream
            client.close(); // close socket
        } // end try
        catch (IOException ioException) {
            ioException.printStackTrace();
        } // end catch
    } // end method closeConnection

    // send message to server
    private void sendData(Communication communicay) {
        try // send object to server
        {
            output.writeObject(communicay);
            output.flush(); // flush data to output
        } // end try
        catch (IOException ioException) {
            ioException.printStackTrace();
        } // end catch
    } // end method sendData
}
/**
 * ************************************************************************
 * (C) Copyright 1992-2010 by Deitel & Associates, Inc. and * Pearson Education,
 * Inc. All Rights Reserved. * * DISCLAIMER: The authors and publisher of this
 * book have used their * best efforts in preparing the book. These efforts
 * include the * development, research, and testing of the theories and programs
 * * to determine their effectiveness. The authors and publisher make * no
 * warranty of any kind, expressed or implied, with regard to these * programs
 * or to the documentation contained in these books. The authors * and publisher
 * shall not be liable in any event for incidental or * consequential damages in
 * connection with, or arising out of, the * furnishing, performance, or use of
 * these programs. *
 ************************************************************************
 */
